package com.fancy.learncenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.learncenter.activity.YgtDetailDecActivity;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.YgtListDataBean;
import com.fancy.learncenter.view.RatingBarView;
import com.superservice.lya.R;

import java.util.ArrayList;

/**
 * Created by hyy on 2018/1/27.
 * Explain activity as
 */

public class YgtListAdapter extends CommonRecycleViewAdapter<YgtListDataBean> {

    String classifyId;

    public YgtListAdapter(Context mContext, ArrayList<YgtListDataBean> itemDatas, String classifyId) {
        super(mContext, R.layout.activity_ygt_list_item, itemDatas);
        this.classifyId = classifyId;
        bgColor.add("#fbfced");
        bgColor.add("#eefcf0");
        bgColor.add("#ecf6fc");
    }

    @Override
    public void bindView(CustomViewHold customViewHold, final YgtListDataBean item, int position) {

        LinearLayout rootView = customViewHold.getView(R.id.rootView);
        rootView.setBackgroundColor(Color.parseColor(bgColor.get((position % 3))));

        RatingBarView ratingBarView = customViewHold.getView(R.id.custom_ratingbar);
        ratingBarView.setClickable(false);
        ratingBarView.setStar(Integer.parseInt(item.getStar()), true);

        TextView title = customViewHold.getView(R.id.title);
        title.setText(item.getName());

        TextView star_num = customViewHold.getView(R.id.star_num);
        star_num.setText(item.getStar() + "星");

        TextView time = customViewHold.getView(R.id.time);
        time.setText(item.getPeriod() + "月");

        TextView money_tv = customViewHold.getView(R.id.money_tv);
        money_tv.setText(item.getSubsidy() + "元");

        TextView description = customViewHold.getView(R.id.description);
        description.setText(item.getFeature());

        TextView peo_num = customViewHold.getView(R.id.peo_num);
        if (item.getPeople_num() > 999) {
            peo_num.setText("999+人选择");
        } else {
            peo_num.setText(item.getPeople_num() + "人选择");
        }


        if (item.getOptionsX() != null) {
            RecyclerView recycleView = customViewHold.getView(R.id.recycleView);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recycleView.setLayoutManager(linearLayoutManager);

            recycleView.setAdapter(new CommonRecycleViewAdapter<YgtListDataBean.OptionsBean>(mContext, R.layout.activity_ygt_list_item_text, (ArrayList<YgtListDataBean.OptionsBean>) item.getOptionsX()) {
                @Override
                public void bindView(CustomViewHold customViewHold, YgtListDataBean.OptionsBean items, int position) {

                    TextView title = customViewHold.getView(R.id.title);
                    title.setText(items.getGoods_option());

                    TextView num = customViewHold.getView(R.id.num);
                    num.setText("¥" + items.getMoney());

                    customViewHold.getRoorView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, YgtDetailDecActivity.class);
                            intent.putExtra("goodsId", item.getId());
                            intent.putExtra("classifyId", classifyId);
                            mContext.startActivity(intent);
                        }
                    });
                }
            });
        }

        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, YgtDetailDecActivity.class);
                intent.putExtra("goodsId", item.getId());
                intent.putExtra("classifyId", classifyId);
                mContext.startActivity(intent);
            }
        });
    }

    ArrayList<String> bgColor = new ArrayList<>();
}
